package com.ruoyi.square.like.service.impl;

import java.util.List;

import com.ruoyi.square.comment.domain.Comment;
import com.ruoyi.square.comment.mapper.CommentMapper;
import com.ruoyi.square.dynamic.mapper.DynamicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.square.like.mapper.LikeMapper;
import com.ruoyi.square.like.domain.Like;
import com.ruoyi.square.like.service.ILikeService;

/**
 * 点赞功能Service业务层处理
 * 
 * @author myd
 * @date 2025-06-07
 */
@Service
public class LikeServiceImpl implements ILikeService 
{
    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private DynamicMapper  dynamicMapper;

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询点赞功能
     * 
     * @param likeId 点赞功能主键
     * @return 点赞功能
     */
    @Override
    public Like selectLikeByLikeId(Long likeId)
    {
        return likeMapper.selectLikeByLikeId(likeId);
    }

    /**
     * 查询点赞功能列表
     * 
     * @param like 点赞功能
     * @return 点赞功能
     */
    @Override
    public List<Like> selectLikeList(Like like)
    {
        return likeMapper.selectLikeList(like);
    }

    /**
     * 新增点赞功能
     * 
     * @param like 点赞功能
     * @return 结果
     */
    @Override
    public int insertLike(Like like)
    {
        // 新增评论
        int rows = likeMapper.insertLike(like);

        // 更新动态评论数
        if (rows > 0 && like.getDynamicId() != null && like.getDynamicId() != 0) {
            dynamicMapper.incrementLikeCount(like.getDynamicId());
        }else if(like.getCommentId() != null && rows > 0 && like.getCommentId() != 0){
            commentMapper.incrementLikeCount(like.getCommentId());
        }
        return rows;
    }

    /**
     * 修改点赞功能
     * 
     * @param like 点赞功能
     * @return 结果
     */
    @Override
    public int updateLike(Like like)
    {
        return likeMapper.updateLike(like);
    }

    /**
     * 批量删除点赞功能
     * 
     * @param likeIds 需要删除的点赞功能主键
     * @return 结果
     */
    @Override
    public int deleteLikeByLikeIds(Long[] likeIds)
    {
        // 先查询要删除的点赞记录
        List<Like> likes = likeMapper.selectLikeListByIds(likeIds);

        // 删除点赞记录
        int rows = likeMapper.deleteLikeByLikeIds(likeIds);

        // 更新动态和评论的点赞数
        if (rows > 0) {
            // 1. 更新动态点赞数
            likes.stream()
                    .filter(like -> like.getDynamicId() != null && like.getDynamicId() != 0)
                    .forEach(like -> {
                        dynamicMapper.decrementLikeCount(like.getDynamicId());
                    });

            // 2. 更新评论点赞数
            likes.stream()
                    .filter(like -> like.getCommentId() != null && like.getCommentId() != 0)
                    .forEach(like -> {
                        commentMapper.decrementLikeCount(like.getCommentId());
                    });
        }

        return rows;
    }

    /**
     * 删除点赞功能信息
     * 
     * @param likeId 点赞功能主键
     * @return 结果
     */
    @Override
    public int deleteLikeByLikeId(Long likeId)
    {
        return likeMapper.deleteLikeByLikeId(likeId);
    }

    @Override
    public Like selectLikeByUserAndContent(Long userId, Long dynamicId, Long commentId) {
        return likeMapper.selectLikeByUserAndContent(userId, dynamicId, commentId);
    }
}
